package org.hzz.freemarker;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.hzz.common.utils.StrUtil;

import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生成常量
 * @author 胖卡
 * @version 1.0.0
 * @date 2024/7/18
 */
public class GeneratorFieldConstants {
    public static void main(String[] args) throws Exception {

        Configuration cfg = new Configuration(Configuration.VERSION_2_3_33);
        // 这里我使用classloader来进行加载文件
        cfg.setClassLoaderForTemplateLoading(
                Thread.currentThread().getContextClassLoader(),
                "templates");
        cfg.setDefaultEncoding("UTF-8");

        // create data model
        Map<String,Object> root = new HashMap<>();
        String tablename = "learn_record";
        root.put("username","胖卡");
        root.put("tablename",tablename);
        root.put("className", StrUtil.toCamelCase(tablename));
        List<String> columns = List.of("id", "course_id", "create_time");
        root.put("columns",columns);

        Template template = cfg.getTemplate("TableFields.ftl");
        // 输出到控制台
        Writer writer = new OutputStreamWriter(System.out);
        template.process(root,writer);
    }
}
